﻿using System;
using System.Collections.Generic;

namespace Asteroids.Entities
{
    public class EntityGroup
    {
        private EntityManager _entityManager;
        private IList<Entity> _entities;
        private string _groupName;

        public EntityGroup(string groupName, EntityManager manager)
        {
            if (manager == null)
                throw new ArgumentNullException("manager", "EntityManager cannot be null.");

            if (string.IsNullOrWhiteSpace(groupName))
                throw new ArgumentNullException("groupName", "groupName cannot be null or empty.");

            _groupName = groupName;
            _entityManager = manager;
            _entities = new List<Entity>();
        }
        
        public ReadOnlyList<Entity> Entities
        { get { return new ReadOnlyList<Entity>(_entities); } }

        public void AddEntity(Entity e)
        {
            if (!_entities.Contains(e))
            { _entities.Add(e); }
        }

        public void RemoveEntity(Entity e)
        {
            if (_entities.Contains(e))
            {
                if (!_entities.Remove(e))
                    throw new InvalidOperationException("This entity could not be removed.");
            }
        }

        public bool Contains(Entity entity)
        {
            return (_entities.Contains(entity));
        }

        public Entity this[int index]
        { 
            get 
            {
                if (index >= _entities.Count || index < 0)
                    throw new IndexOutOfRangeException("The index is out of range.");

                return _entities[index];
            } 
        }


        public int Count
        { get { return _entities.Count; } }

        public string Name
        { get { return _groupName; } }
    }
}
